package com.bing;

import com.bing.entity.HEntity;
import com.bing.entity.JPAEvent;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import java.util.Date;
import java.util.List;

/**
 * Created by kaibb on 15-10-30.
 */
public class HelloJpa {
    private EntityManagerFactory entityManagerFactory;
    private EntityManager entityManager;

    @Before
    public void setUp() throws Exception {
        // like discussed with regards to SessionFactory, an EntityManagerFactory is set up once for an application
        // 		IMPORTANT: notice how the name here matches the name we gave the persistence-unit in persistence.xml!
        entityManagerFactory = Persistence.createEntityManagerFactory("jpa");
    }

    @After
    public void tearDown() throws Exception {
        entityManagerFactory.close();
    }

    @Test
    public void hello() {
        entityManager = entityManagerFactory.createEntityManager();
        entityManager.getTransaction().begin();
        entityManager.persist(new JPAEvent("Our very first event!", new Date()));
        entityManager.persist(new JPAEvent("A follow up event", new Date()));
        entityManager.getTransaction().commit();
        entityManager.close();

        // now lets pull events from the database and list them
        entityManager = entityManagerFactory.createEntityManager();
        entityManager.getTransaction().begin();
        List<JPAEvent> result = entityManager.createQuery("from JPAEvent", JPAEvent.class).getResultList();
        for (JPAEvent event : result) {
            System.out.println("Event (" + event.getDate() + ") : " + event.getName());
        }
        entityManager.getTransaction().commit();
        entityManager.close();
    }


    @Test
    public void helloHEntity() {
        entityManager = entityManagerFactory.createEntityManager();
        entityManager.getTransaction().begin();
        HEntity hEntity = new HEntity();
        hEntity.getAttributes().put("name", "kaibing");
        hEntity.getAttributes().put("password", "2222.com");

        entityManager.persist(hEntity);

        HEntity hEntity2 = new HEntity();
        hEntity2.getAttributes().put("name", "kaibing2");
        hEntity2.getAttributes().put("password", "2222.com");

        entityManager.persist(hEntity2);

        HEntity hEntity3 = new HEntity();
        hEntity3.getAttributes().put("mingzi", "kaibing2");
        hEntity3.getAttributes().put("mima", "2222.com");

        entityManager.persist(hEntity3);
        entityManager.getTransaction().commit();
        entityManager.close();

    }

    @Test
    public void select(){
        entityManager = entityManagerFactory.createEntityManager();
        entityManager.getTransaction().begin();
        List<HEntity> resultList = entityManager.createQuery("from HEntity where attributes['name'] like 'kaibing%'", HEntity.class).getResultList();
        for (HEntity hEntity : resultList) {
            System.out.println(hEntity);
        }
        entityManager.getTransaction().commit();
        entityManager.close();
    }
}
